package com.huike.web.controller.review;


import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**=========================================================新增数据============================================*/
    @PostMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult save(@PathVariable String name, @PathVariable Integer age, @PathVariable String sex) {
        Review review = new Review();
        review.setName(name);
        review.setAge(age);
        review.setSex(sex);
        System.out.println(review);
        int row = reviewService.save(review);
        if (row > 0) {
            return AjaxResult.success("成功插入:1条数据");
        }

        return AjaxResult.success("插入失败");
    }

    @PostMapping("/saveData")
    public AjaxResult saveData(@RequestBody Review review) {
        System.out.println(review);
        int row = reviewService.save(review);
        if (row > 0) {
            return AjaxResult.success("成功插入:1条数据");
        }

        return AjaxResult.success("插入失败");
    }

    /**=========================================================删除数据=============================================*/
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Integer id) {
        int row = reviewService.remove(id);
        if (row > 0) {
            return AjaxResult.success("删除成功");
        }

        return AjaxResult.success("删除失败");
    }
    /**=========================================================修改数据=============================================*/
    @PutMapping("/update")
    public AjaxResult update(@RequestBody Review review) {
        // System.out.println(review);
        int row = reviewService.update(review);
        if (row > 0) {
            return AjaxResult.success("修改成功");
        }

        return AjaxResult.success("修改失败");
    }
    /**=========================================================查询数据=============================================*/
    @GetMapping("/getById")
    public AjaxResult getById(Integer id) {
        Review review = reviewService.getById(id);
        if (review != null) {
            return AjaxResult.success("获取成功", review);
        }
        return AjaxResult.success("获取失败");
    }

    @GetMapping("/getDataByPage")
    public TableDataInfo getDataByPage() {
/*        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        List<Review> reviewList = reviewService.getDataByPage(pageDomain);
        System.out.println("size:"+reviewList.size());

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        int total = reviewList.size();
        if (total > pageSize) {
            int toIndex = pageSize * pageNum;
            if (toIndex > total) {
                toIndex = total;
            }
            reviewList = reviewList.subList(pageSize * (pageNum - 1), toIndex);
        }

        rspData.setRows(reviewList);
        rspData.setTotal(total);
        return rspData;*/
        startPage();
        List<Review> reviewList = reviewService.getDataByPage();
        return getDataTable(reviewList);
    }
}